<template>
  <div class="body-container layout-padding">
    <div style="padding: 10px">
      <el-form label-width="120px" style="margin-bottom: 10px">
        <el-row :gutter="20">
          <el-col :span="14">
            <el-form-item label="客户名称">
              <el-input v-model="state.sd.searchName" placeholder="客户名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button size="default" type="primary" class="ml10" @click="searchUser">
              <el-icon><ele-Search /></el-icon>查询
            </el-button>
            <el-button size="default" type="primary" class="ml10" @click="addUser">
              <el-icon><ele-CirclePlus /></el-icon>添加
            </el-button>
          </el-col>
        </el-row>
      </el-form>

      <el-table :data="state.userObj.list" border style="width: 100%">
        <el-table-column label="客户名称" prop="name"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="手机号" prop="phone"></el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button size="small" type="primary" @click="editUser(scope.row.id)">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination class="mt15" background layout="total, prev, pager, next"
                     @current-change="loadUser" @size-change="loadUser" :total="state.userObj.total"
                     v-model:current-page="state.sd.pageNum" v-model:page-size="state.sd.pageSize">
      </el-pagination>
      <el-dialog :title="state.popupTitle" v-model="state.entityDetailVisible" :close-on-click-modal="false" width="800px">
        <KeHuDetail v-if="state.entityDetailVisible" v-model="state.entityDetailVisible" :entityId="state.popupEntityId"></KeHuDetail>
      </el-dialog>
    </div>
  </div>
</template>

<script setup name="kehuIndex">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import {reactive, ref, onMounted, onUnmounted, defineAsyncComponent} from "vue";
// 引入组件
const KeHuDetail = defineAsyncComponent(() => import('/@/views/sys/user/kehuDetail.vue'));

const state = reactive({
  sd: {
    searchName : '',
    pageNum: 1,
    pageSize: 15
  },
  userObj: {},

  entityDetailVisible : false,
  popupTitle : '',
  popupEntityId : 0,
})
const searchUser = ()=>{
  state.sd.pageNum = 1;
  loadUser();
}
const loadUser = ()=>{
  request({url: '/sys/kehuadmin/pagekehu', data: state.sd}).then(res => {
    const { data } = res
    state.userObj = data
  })
}
const addUser = ()=>{
  state.popupEntityId = 0;
  state.popupTitle= "新增客户用户"
  state.entityDetailVisible= true
}
const editUser = (id)=>{
  state.popupEntityId = id;
  state.popupTitle= "修改客户用户"
  state.entityDetailVisible= true
}
onMounted(()=>{
  loadUser();
  mittBus.on("kehuadd", ()=>{
    state.sd.pageNum = 1;
    loadUser();
  })
  mittBus.on("kehuedit", ()=>{
    loadUser();
  })
})
onUnmounted(()=>{
  mittBus.off("kehuadd", ()=>{})
  mittBus.off("kehuedit",()=>{})
})
</script>

<style scoped>

</style>
